package com.qianfeng.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.qianfeng.shopping.dao.LocationDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LocationServlet", value = "/LocationServlet")
public class LocationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");
        LocationDao locationDao = new LocationDao();
        response.setHeader("Context-type","text/plain,charset=utf-8");
        PrintWriter pw = response.getWriter();
        String str ="";
        // 获取省份的code
        String code = request.getParameter("proCode");
        // 获取城市codie
        String cityCode = request.getParameter("cityCode");
        if ("selectAllPro".equals(flag)) {
            str =  JSON.toJSONString(locationDao.selectAllPro());
        }else if ("selectCity".equals(flag)) {
            str =  JSON.toJSONString(locationDao.selectCity(Integer.parseInt(code)));
        }else if ("selectArea".equals(flag)) {
            str =  JSON.toJSONString(locationDao.selectArea(Integer.parseInt(cityCode)));
        }
        pw.write(str);
        pw.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
